package com.wude.guild;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AppInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.DownloadActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.GameDetFragmentAdapter;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.guild.views.RoundAngleImageView;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("HandlerLeak")
public class GameDetActivity extends TitleBaseActivity implements View.OnClickListener {
    @BindView(R.id.home_game_icon)
    RoundAngleImageView homeGameIcon;
    @BindView(R.id.home_game_name)
    TextView homeGameName;
    @BindView(R.id.home_game_ratingBar)
    RatingBar homeGameRatingBar;
    @BindView(R.id.game_size)
    TextView gameSize;
    @BindView(R.id.down_num)
    TextView downNum;
    @BindView(R.id.jianjie)
    TextView jianjie;
    @BindView(R.id.zhishu)
    RelativeLayout zhishu;
    @BindView(R.id.zhekou)
    TextView zhekou;
    @BindView(R.id.btn_jianjie)
    RadioButton btnJianjie;
    @BindView(R.id.btn_gift)
    RadioButton btnGift;
    @BindView(R.id.btn_startfu)
    RadioButton btnStartfu;
    @BindView(R.id.btn_pinglun)
    RadioButton btnPinglun;
    @BindView(R.id.btn_group)
    RadioGroup btnGroup;
    @BindView(R.id.line)
    ImageView line;
    @BindView(R.id.kf_viewpager)
    ViewPager kfViewpager;
    @BindView(R.id.fanli)
    TextView fanli;
    private int screenWidth;
    private String id;
    private AppInfo appInfo;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_det);
        ButterKnife.bind(this);

        ImageView rightImageView = getTitleHeaderBar().getRightImageView();
        rightImageView.setImageResource(R.mipmap.game_btn_download);
        rightImageView.setOnClickListener(this);

        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        boolean comment = getIntent().getBooleanExtra("comment", false);
        kfViewpager.setOffscreenPageLimit(2);
        initdata();
        HashMap<String, String> map = new HashMap<>();
        map.put("game_id", id);
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            map.put("token", loginUser.token);
        }
        Log.e("游戏ID", id);
        HttpCom.POST(handler, HttpCom.getGameDetUrl(), map, false);
        if (comment)
            kfViewpager.setCurrentItem(3);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initdata() {
        //获取屏幕的宽度
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;

        //设置mTabLine宽度//获取控件的(注意：一定要用父控件的LayoutParams写LinearLayout.LayoutParams)
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) line.getLayoutParams();//获取控件的布局参数对象
        lp.width = screenWidth / 4;
        line.setLayoutParams(lp); //设置该控件的layoutParams参数

        if (id != null) {
            GameDetFragmentAdapter kaiFuFragmentAdapter = new GameDetFragmentAdapter(getSupportFragmentManager(), id);
            kfViewpager.setAdapter(kaiFuFragmentAdapter);
        } else {
            GameDetFragmentAdapter kaiFuFragmentAdapter = new GameDetFragmentAdapter(getSupportFragmentManager(), id);
            kfViewpager.setAdapter(kaiFuFragmentAdapter);
        }

        btnGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.btn_jianjie:
                        kfViewpager.setCurrentItem(0);
                        break;
                    case R.id.btn_gift:
                        kfViewpager.setCurrentItem(1);
                        break;
                    case R.id.btn_startfu:
                        kfViewpager.setCurrentItem(2);
                        break;
                    case R.id.btn_pinglun:
                        kfViewpager.setCurrentItem(3);
                        break;

                }
            }
        });


        kfViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        btnJianjie.setChecked(true);
                        break;
                    case 1:
                        btnGift.setChecked(true);
                        break;
                    case 2:
                        btnStartfu.setChecked(true);
                        break;
                    case 3:
                        btnPinglun.setChecked(true);
                        break;

                }
            }

            /**
             * 当前页面被滑动时调用
             * position:当前页面
             * positionOffset:当前页面偏移的百分比
             * positionOffsetPixels:当前页面偏移的像素位置
             */
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) line.getLayoutParams();
                //获取组件距离左侧组件的距离
                lp.leftMargin = (int) ((positionOffset + position) * screenWidth / 4);
                line.setLayoutParams(lp);
            }


            @Override
            public void onPageScrollStateChanged(int position) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_image:
                startActivity(new Intent(this, DownloadActivity.class));
                break;
        }
    }

    /**
     * 游戏详情
     */

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    appInfo = HttpUtils.DNSGameDel(msg.obj.toString());
                    if (appInfo != null) {
                        MCUtils.fillImage(homeGameIcon, appInfo.iconurl);
                        String jieQu = Utils.getJieQu(appInfo.name);
                        homeGameName.setText(jieQu);
                        gameSize.setText(appInfo.size);
                        homeGameRatingBar.setRating(appInfo.rating);
                        jianjie.setText(appInfo.features);
                        downNum.setText(appInfo.DownNum + "人下载");
                        setHeaderTitle(jieQu);
                        if (type != null && type.equals("折扣")) {
                            zhekou.setText(appInfo.discount + "");
                            fanli.setText("折");
                        } else {
                            zhekou.setText("返利");
                            int i = appInfo.fanli.indexOf(".00");
                            if (i != -1) {
                                String substring = appInfo.fanli.substring(0, i);
                                fanli.setText(substring + "%");
                            } else {
                                fanli.setText(0 + "%");
                            }

                        }
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
